package com.hibase.core.excel.controller;

import com.hibase.common.constant.ResponseCode;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.excel.annotation.HibaseRequestExcel;
import com.hibase.core.excel.data.HibaseAnalysisProcessor;
import com.hibase.core.excel.entity.MdsExcelImportConfig;
import com.hibase.core.excel.entity.MdsExcelImportLog;
import com.hibase.core.excel.service.MdsExcelImportConfigService;
import com.hibase.core.excel.service.MdsExcelImportLogService;
import com.hibase.core.excel.vo.dynamic.DynamicExcelResult;
import com.hibase.core.oss.controller.AttachmentPreviewController;
import com.hibase.core.web.annotation.HibaseRequestMapping;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 上传Excel通用接口
 *
 * @author <a href="mailto:xiongliyi@hichain.com">Ian Xiong</a>
 * @since 1.4.50-V2
 */
@Slf4j
@AllArgsConstructor
@Api(value = "mds-excel", tags = "Excel自定义配置上传接口")
@RequestMapping("/mds-excel")
@RestController
public class MdsExcelController {
    private final MdsExcelImportConfigService mdsExcelImportConfigService;
    // from 附件模块
    private final AttachmentPreviewController attachmentPreviewController;
    private final MdsExcelImportLogService mdsExcelImportLogService;

    @ApiOperation(value = "Excel数据导入", httpMethod = "POST", response = ResponseModel.class)
    @RequestMapping(value = "/importExcel", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "multipart/form-data")
    @ApiResponses({@ApiResponse(code = 0, message = "上传成功"), @ApiResponse(code = 999, message = "上传失败")})
    @ApiImplicitParams({@ApiImplicitParam(name = "templateCode", value = "模板编码", required = true, dataType = "String", paramType = "path"), @ApiImplicitParam(name = "ignoreEmptyRow", value = "是否跳过空行", required = false, dataType = "Boolean", paramType = "path"), @ApiImplicitParam(name = "headRowNumber", value = "列头行号", required = false, dataType = "Integer", paramType = "path"), @ApiImplicitParam(name = "file", value = "文件对象", required = true, dataType = "__file", paramType = "path")})
    public ResponseModel upload(@HibaseRequestExcel(fileName = "file", requireTemplateCode = true, readAnalysisProcessor = HibaseAnalysisProcessor.class) DynamicExcelResult dynamicExcelResult) {

        MdsExcelImportLog mdsExcelImportLog = EntityUtil.transform(dynamicExcelResult.getConfigInfo(), MdsExcelImportLog.class);
        try {

            Object result = mdsExcelImportConfigService.importExcel(dynamicExcelResult);
            mdsExcelImportLog.setBeSuccess(Boolean.TRUE);
            mdsExcelImportLogService.save(mdsExcelImportLog);

            return ResponseModel.successMsg(result);
        } catch (Exception ex) {
            BindingResult bindingResult = dynamicExcelResult.getBindingResult();
            // TODO: 记录错误信息


            mdsExcelImportLog.setErrMessage(ex.getMessage());
            mdsExcelImportLog.setBeSuccess(Boolean.FALSE);
            mdsExcelImportLogService.save(mdsExcelImportLog);

            return ResponseModel.failMsg(ResponseCode.SERVER_ERROR.getCode(), "Import failed because of" + ex.getMessage());
        }
    }

    @ApiOperation(value = "下载Excel模板文件", httpMethod = "POST", response = ResponseModel.class)
    @HibaseRequestMapping(value = "/downloadTemplate", method = {RequestMethod.GET, RequestMethod.POST})
    public void downloadTemplate(@RequestBody String bizTemplateCode, HttpServletResponse response) {
        MdsExcelImportConfig config = mdsExcelImportConfigService.selectByCode(bizTemplateCode);
        attachmentPreviewController.downloadFile(config.getTemplatePath(), response);
    }

}
